<template>
  <div
    class="common-custom-nav-head"
    :class="[
      theme,
      {
        shadow: shadow && pageOffsetTop > 90,
        fixed: fixed,
      },
    ]"
    :style="{
      paddingTop: pageNavRect.top + 'px',
    }"
  >
    <div
      class="nav-bg"
      :style="{
        backgroundColor: navBgColor,
        opacity: pageOffsetTop > 90 ? 1 : pageOffsetTop / 90,
      }"
    ></div>
    <div class="nav-head" :style="{ height: pageNavRect.titleHeight + 'px' }">
      <div v-if="isBack" class="icon-back" @click="onBack"></div>
      <div class="text-title" :style="{color: !fixed ? '' : fixColor}">{{ title }}</div>
    </div>
    <div class="nav-body">
      <slot></slot>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'CommonCustomNavHead',
    options: {
      styleIsolation: 'apply-shared',
    },
    props: {
      title: String,
      // 主题 dark、light
      theme: {
        type: String,
        default: 'light',
      },
      // 页面滚动偏离顶部的距离，值大于90时，完整显示导航栏背景色
      pageOffsetTop: {
        type: Number,
        default: 0,
      },
      // 页面滚动偏离顶部的距离，值大于90时，完整显示导航栏背景色后，标题栏颜色
      fixColor: {
        type: String,
        default: '#1f2329',
      },
      // 返回上一页的页面path
      prePage: String,
      // 返回上一页是否是tab页
      isTab: Boolean,
      // 是否显示阴影
      shadow: {
        type: Boolean,
        default: true,
      },
      // 导航栏背景色
      navBgColor: {
        type: String,
        default: '#ffffff',
      },
      // 是否显示返回按钮
      isBack: {
        type: Boolean,
        default: true
      }
    },
    data() {
      return {
        // 自定义header布局位置信息
        pageNavRect: this.$systemTools.pageNavRect,
      };
    },
    computed: {
      // 导航栏是否固定
      fixed() {
        return this.pageOffsetTop > 15;
      },
    },
    watch: {
      fixed(val) {
        if (this.theme === 'dark') {
          return;
        }
        let frontColor = '#ffffff';
        if (val) {
          frontColor = '#000000';
        }
        // 设置页面导航条颜色
        wx.setNavigationBarColor({
          frontColor,
          backgroundColor: '#ffffff',
        });
      },
    },
    methods: {
      onBack() {
        const pages = getCurrentPages();
        if (pages.length === 1) {
          wx.reLaunch({ url: this.prePage });
        } else {
          if (this.prePage && this.isTab) {
            uni.switchTab({
              url: this.prePage
            })
          } else if (this.prePage && !this.isTab) {
            uni.navigateTo({
              url: this.prePage
            })
          } else {
            wx.navigateBack();
          }
        }
      },
    },
  };
</script>

<style lang="less">
  .common-custom-nav-head {
    position: fixed;
    top: 0;
    right: 0;
    left: 0;
    z-index: 999;

    &.shadow {
      box-shadow: 0 4px 4px 0 rgba(201, 206, 218, 0.25);
    }

    &.dark {
      .nav-head {
        .icon-back {
          background-image: url('https://image.doulaoban.com/applet/image/1703081819379_2.png');
        }

        .text-title {
          color: #1f2329;
        }
      }
    }

    &.fixed {
      .nav-head {
        .icon-back {
          background-image: url('https://image.doulaoban.com/applet/image/1703081819379_2.png');
        }

        .text-title {
          color: #1f2329;
        }
      }
    }

    .nav-bg {
      position: absolute;
      inset: 0;
      z-index: -1;
      pointer-events: none;
    }

    .nav-head {
      position: relative;
      display: flex;
      align-items: center;

      .icon-back {
        position: absolute;
        top: 0;
        bottom: 0;
        left: 14px;
        width: 20px;
        height: 20px;
        margin: auto;
        background-image: url('https://image.doulaoban.com/applet/image/1703081819380_1.png');
        background-repeat: no-repeat;
        background-position: center;
        background-size: 16px 16px;
      }

      .text-title {
        width: 100%;
        font-size: 18px;
        font-weight: 500;
        line-height: 1;
        color: #fff;
        text-align: center;
      }
    }
  }
</style>
